package cwinsor.com.leaf_raking;

import java.util.Arrays;
import java.util.List;

public final class AmPm {

	/*
	 * properties
	 */
	private String name;

	/*
	 * getters
	 */
	public final String name() { return name; };

	/*
	 * private constructor
	 */
	private AmPm(String n) { name = n; }

	/*
	 * global instances
	 */
	public static final AmPm AM = new AmPm("AM");
	public static final AmPm PM = new AmPm("PM");

	/*
	 * global instances as a list (public method)
	 */
	public static final List<AmPm> asList() {
		return instanceList;		
	}

	/*
	 * instances list (private)
	 */
	private static final List<AmPm> instanceList = Arrays.asList(
			AM,
			PM);

	/*
	 * number of instances
	 */
	public static final Integer size() { return instanceList.size(); };

	/*
	 * fromString
	 * converts string to AmPm
	 * fails if it finds zero or more than one
	 */
	public static AmPm fromString(String string) {
		Integer count = 0;
		AmPm theRtn = null;
		for (AmPm amPm : AmPm.asList()) {
			if (string.contains(amPm.name())) {
				theRtn = amPm;
				count++;
			}
		}
		// sanity check - there should be exactly one
		if (!count.equals(1)) {
			Msg.fatal(dS + ": expected exactly one but there were " + count + " string was " + string);
		}
		return theRtn;
	}


	// debug
	private static String dS = "AmPm";
}

